#!/bin/sh

# File: create_mail_ldap_user.sh
# Version: 0.3

# Author: Bibby(michaelbibby#gmail.com)
# Date: 2007.11.18
# Purpose: Add new LDAP user for postfix mail server.

# Use vim:
#    :set tabstop=4
#    :set shiftwidth=4
#

# Changelog:
# 0.3 -> 0.3.1
#   * modify LDIF. delete 'organizationalPerson' objectclass.
# 0.2 -> 0.3
#   * fix: maildir directory error.
#   * add new domain if not exist.
# 0.1.2 -> 0.2
#    * you must specify 'DOMAIN_NAME' now.
# 0.1.1 -> 0.1.2
#    * use 'maildirmake' to create maildir before send welcome mail, so that
#      maildrop can work correctly.
# 0.1 -> 0.1.1:
#   * Add maildrop/quota support.
# 0.1: init version.

# ----------------------------------------------
# ------------ Global Setting ------------------
# ----------------------------------------------
MAIL_OWNER_USER="vmail"
MAIL_OWNER_GROUP="vmail"

# BASE_DN, DOMAIN_DN:
# The full DN will be:
#
#     uid=${USERNAME}, ${DOMAIN_DN}, ${BASE_DN}
#
# such as:
#
#    uid=bibby, o=domain1.com, o=domains,dc=bibby,dc=org
#       _____    ___________  _________________________
#        /|\        /|\                  /|\
#         |          |                    |
#      USERNAME   DOMAIN_NAME          BASE_DN
#

LDAP_SUFFIX="dc=openbsdonly,dc=org"

# Setting 'BASE_DN'.
BASE_DN="o=domains,${LDAP_SUFFIX}"

# Setting 'DOMAIN_NAME' and DOMAIN_DN':
#     * DOMAIN will be used in mail address: ${USERNAME}@${DOMAIN}
#    * DOMAIN_DN will be used in LDAP dn.
DOMAIN_NAME="$1"
DOMAIN_DN="o=${DOMAIN_NAME}"

# ---------- rootdn of LDAP Server ----------
# Setting rootdn of LDAP.
ROOTDN="cn=Manager,${LDAP_SUFFIX}"

# Setting rootpw of LDAP.
ROOTPW="passwd"
 
# ---------- LDAP User Setting --------------
# Setting Maildir name.
MAILDIR='Maildir'

# HOME directory for LDAP user.
# mailbox of LDAP user will be:
#    ${VMAIL_USER_HOME_DIR}/${DOMAIN_NAME}/${USERNAME}/Maildir/
# Such as:
#    /home/vmail/domains/domain1.com/bibby/Maildir/
#   -------------------|===========|-----|
#   VMAIL_USER_HOME_DIR|DOMAIN_NAME|USERNAME
#
VMAIL_USER_HOME_DIR="/home/vmail/domains"

# Set default quota for LDAP users.
# 10240000S = 10240000 Bit = 10M
QUOTA='10240000S'

# ---------- Welcome Mail info -------------
# Set welcome mail info.
WELCOME_MSG_TITLE="Welcome!"
WELCOME_MSG_BODY="Welcome, new user."

# -------------------------------------------
# ----------- End Global Setting ------------
# -------------------------------------------

add_new_domain()
{
    ldapsearch -x -D "${ROOTDN}" -w "${ROOTPW}" \
    -b "${BASE_DN}" | \
    grep "o: ${DOMAIN_NAME}" >/dev/null

    if [ X"$?" != X"0" ]; then
        echo "Add new domain: ${DOMAIN_NAME}."

        ldapadd -x -D "${ROOTDN}" -w "${ROOTPW}" <<EOF
dn: o=${DOMAIN_NAME}, ${BASE_DN}
objectClass: Organization
o: ${DOMAIN_NAME}
description: maildrop
EOF
    fi
}

add_new_user()
{
    USERNAME="$1"
    MAIL="$2"

    # Create template LDIF file for this new user and add it.
    # If you do *NOT* want to keep rootpw in script, use '-W' instead of 
    # '-w "${ROOTPW}".
    ldapadd -x -D "${ROOTDN}" -w "${ROOTPW}" <<EOF
dn: mail=${MAIL}, ${DOMAIN_DN}, ${BASE_DN}
objectClass: inetOrgPerson
objectClass: qmailUser
objectClass: top
homeDirectory: ${VMAIL_USER_HOME_DIR}
accountStatus: active
mailMessageStore: ${DOMAIN_NAME}/${USERNAME}/${MAILDIR}/
mail: ${MAIL}
mailQuota: ${QUOTA}
userPassword: ${USERNAME}
cn: ${USERNAME}
sn: ${USERNAME}
uid: ${USERNAME}
EOF
}

create_maildir()
{
    DOMAIN_NAME="$1"
    USERNAME="$2"

    user_home="${VMAIL_USER_HOME_DIR}/${DOMAIN_NAME}/${USERNAME}/"
    user_maildir="${VMAIL_USER_HOME_DIR}/${DOMAIN_NAME}/${USERNAME}/${MAILDIR}"
    # Use 'maildirmake' to create Maildir before send welcome mail to user.
    echo "Create Maildir: ${user_maildir}."
    mkdir -p ${user_home}
    maildirmake ${user_maildir}

    print "INBOX.Sent" > ${user_maildir}/courierimapsubscribed
    print "INBOX.Trash" >> ${user_maildir}/courierimapsubscribed
    print "INBOX.Drafts" >> ${user_maildir}/courierimapsubscribed
    print "INBOX.Spam" >> ${user_maildir}/courierimapsubscribed

    chown -R ${MAIL_OWNER_USER}:${MAIL_OWNER_GROUP} ${VMAIL_USER_HOME_DIR}/${DOMAIN_NAME}/${USERNAME}/
    chmod -R 700 ${VMAIL_USER_HOME_DIR}/${DOMAIN_NAME}/${USERNAME}/
}

send_welcome_mail()
{
    MAIL="$1"
    echo "Send a welcome mail to new user: ${MAIL}"

    echo "${WELCOME_MSG_BODY}" | mail -s "${WELCOME_MSG_TITLE}" ${MAIL}
}

print_usage()
{
    echo "Usage:"
    echo -e "\t$0 DOMAIN USERNAME"
    echo -e "\t$0 DOMAIN USER1 USER2 USER3..."
}

if [ $# -lt 2 ]; then
    print_usage
else
    DOMAIN_NAME="$1"
    shift 1

    add_new_domain ${DOMAIN_NAME}
    for i in $@
    do
        USERNAME="$i"
        MAIL="${USERNAME}@${DOMAIN_NAME}"

        add_new_user ${USERNAME} ${MAIL} && \
        create_maildir ${DOMAIN_NAME} ${USERNAME} && \
        send_welcome_mail ${MAIL}
    done
fi
